package com.bw.listener;

import com.bw.pojo.Coupon;
import com.bw.service.Couponervice;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * rabiitMQ监听控制
 */
@Component
public class RabbitMQListener {
    //spring注入service业务对象
    @Autowired
    private Couponervice couponervice;

    //接受消息
    @RabbitListener(queues = "bw")
    public void sendMessage(Integer cid){
        //调用service业务对象 根据优惠券id获取优惠券详细信息
        Coupon dbCoupon = couponervice.getById(cid);
        //优惠券库存-1
        dbCoupon.setNum(dbCoupon.getNum()-1);
        //调用service业务对象 修改该优惠券的库存
        couponervice.updateById(dbCoupon);
        System.out.println("优惠券编号:"+cid+"库存-1,   剩余库存:  "+dbCoupon.getNum()+"张");
    }
}
